package com.sdruixinggroup.mondayb2b.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sdruixinggroup.mondayb2b.API.API;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.interfaces.ObjectCallBack;
import com.sdruixinggroup.mondayb2b.models.ModelBeen;
import com.sdruixinggroup.mondayb2b.utils.UserDate;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import okhttp3.Call;

/**
 * Created by tangjie on 2017/11/11.
 */

public class CommitNameActivity extends BaseActivity implements View.OnClickListener {
    private TextView topbar_center, topbar_right;
    private ImageView topbar_left;
    private Intent intent;
    private int code;
    private EditText commit;
    private Button del;
    private String nickname;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commit);

        intent = getIntent();
        code = intent.getIntExtra("code", 0);

        initView();

    }

    private void initView() {
        topbar_center = (TextView) findViewById(R.id.topbar_title);
        topbar_right = (TextView) findViewById(R.id.topbar_right);
        topbar_right.setText("保存");
        topbar_left = (ImageView) findViewById(R.id.topbar_left);
        commit = (EditText) findViewById(R.id.commit_edit);
        if (code == 0) {
            topbar_center.setText("修改昵称");
            nickname = intent.getStringExtra("nickname");
            commit.setText(nickname);
            commit.setSelection(commit.length());
        } else {
            topbar_center.setText("修改用户名");
        }

     /*   if (code == 0) {
            commit.setText(UserDate.getName(this));
        } else {
            commit.setText(UserDate.getUserName(this));

        }*/
        del = (Button) findViewById(R.id.commit_del);

        registerListener();

    }

    private void registerListener() {
        topbar_right.setOnClickListener(this);
        topbar_left.setOnClickListener(this);
        del.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == topbar_right) {
          String name= commit.getText().toString().trim();
            if (code == 0) {
                UserDate.setName(this, commit.getText().toString());
                if(!TextUtils.isEmpty(name)){
                    changeNick(name);
                }else {
                    showToast("昵称不能为空");
                }

            } else {
                UserDate.setUserName(this, commit.getText().toString());
            }
        }
        if (view == topbar_left) {
            finish();
        }
        if (view == del) {
            commit.setText("");
        }

    }


    private void changeNick(String nick){
        String url = API.NICK_NAME+nick +"&access_token=" + UserInfoUtil.getInfoToken(getApplicationContext());
        HashMap<String,String> headers = new HashMap<>();
        headers.put("Accept","*/*");
        TypeToken<ModelBeen> typeToken = new TypeToken<ModelBeen>(){};
        OkHttpUtils.get().url(url).headers(headers).build().execute(new ObjectCallBack<ModelBeen>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(ModelBeen response, int id) {
                        if(null != response){
                            if(response.err_code == 0){
                                showToast("修改成功！");
                                EventBus.getDefault().post(123);
                                finish();
                            }else {
                                showToast(response.err_msg);
                            }
                        }
            }
        });
    }
}
